package com.lht.syt.hosp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lht.syt.common.exception.MyException;
import com.lht.syt.common.result.ResultCodeEnum;
import com.lht.syt.hosp.mapper.HospitalSetMapper;
import com.lht.syt.hosp.service.HospitalSetService;
import com.lht.syt.model.hosp.HospitalSet;
import com.lht.syt.vo.order.SignInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lht
 * @version 1.0
 * @description: TODO
 * @date 2024/10/31 17:50
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

    @Autowired
    private HospitalSetMapper hospitalSetMapper;

    @Override
    public String getSignKey(String hoscode) {
        HospitalSet hospitalSet = this.getByHospCode(hoscode);
        if (null == hospitalSet) {
            throw new MyException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        if (0 == hospitalSet.getStatus().intValue()) {
            throw new MyException(ResultCodeEnum.HOSPITAL_LOCK);
        }
        return hospitalSet.getSignKey();
    }

    //获取医院签名信息
    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode",hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(wrapper);
        if(null == hospitalSet) {
            throw new MyException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        signInfoVo.setSignKey(hospitalSet.getSignKey());
        return signInfoVo;
    }

    private HospitalSet getByHospCode(String hosCode) {
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("hoscode", hosCode);
        HospitalSet hospitalSet = this.hospitalSetMapper.selectOne(queryWrapper);
        return hospitalSet;
    }
}
